home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_m_p / mwpropvw.zip / MEWLDEMO.C < prev    next >
C/C++ Source or Header  |  1992-01-25  |  53KB  |  1,767 lines

  1. #if 0
  2. #define MODELESSTEST
  3. #define CTLCOLOR_TEST
  4. #endif
  5.  
  6. /*===========================================================================*/
  7. /*                                                                           */
  8. /* File    : MEWLDEMO.C                                                      */
  9. /*                                                                           */
  10. /* Purpose : General purpose demo of the MEWEL Window Library                */
  11. /*                                                                           */
  12. /* History :                                                                 */
  13. /*                                                                           */
  14. /* (C) Copyright 1989 Marc Adler/Magma Systems     All Rights Reserved       */
  15. /*===========================================================================*/
  16.  
  17. #include <ctype.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <limits.h>
  22. #ifndef UNIX
  23. #include <io.h>
  24. #include <dos.h>
  25. #endif
  26. #include <windows.h>
  27.  
  28. #ifdef MEWEL
  29. #define MAKE_X(n)   (n * 1)
  30. #define MAKE_Y(n)   (n * 1)
  31. #define CX_RECT(r)  (r.right  - r.left + 1)
  32. #define CY_RECT(r)  (r.bottom - r.top  + 1)
  33. #else
  34. #define MAKE_X(n)   (n * 8)
  35. #define MAKE_Y(n)   (n * 16)
  36. #define CX_RECT(r)  (r.right  - r.left)
  37. #define CY_RECT(r)  (r.bottom - r.top)
  38.  
  39. #define VidSetBlinking(n)
  40. #define WM_HELP     (WM_USER + 100)
  41. #endif
  42.  
  43.  
  44. typedef long (PASCAL *TIMERPROC)();
  45.  
  46. #define ID_MENUBAR    10
  47. #define ID_NOTEPAD    12
  48. #define ID_ABOUT      98
  49. #define ID_COMBOBOX   97 
  50. #define ID_EXIT       99
  51.  
  52. #define ID_LISTDEMO   200
  53. #define ID_TIMEDEMO   201
  54. #define ID_MOUSEDEMO  202
  55. #define ID_MENUDEMO   203
  56. #define ID_FILEDEMO   204
  57. #define ID_EDITDEMO   205
  58. #define ID_EXECDEMO   206
  59. #define ID_CALCDEMO   207
  60. #define ID_CHILDDEMO  208
  61. #define ID_GRAPHICS   213
  62. #define ID_TOGGLEEGA  214
  63.  
  64. #define ID_COMBODEMO  210
  65.  
  66.  
  67. #define ID_STRING     100
  68. #define ID_POS        101
  69. #define ID_FRAME      102
  70. #define ID_APPEND     103
  71. #define ID_INSERT     104
  72. #define ID_DELETE     105
  73. #define ID_LISTBOX    106
  74.  
  75. #define ID_STOP       100
  76. #define ID_START      101
  77. #define ID_INTERVAL   105
  78.  
  79. #define ID_WORD50     300
  80. #define ID_WRITE      301
  81. #define ID_EXCEL      302
  82. #define ID_124        303
  83. #define ID_C52        304
  84. #define ID_QUICKC     305
  85. #define ID_QUICKBASIC 306
  86. #define ID_PASCAL     307
  87.  
  88. #define ID_NEW    200
  89. #define ID_OPEN   201
  90. #define ID_SAVE   202
  91. #define ID_SAVEAS 203
  92. #define ID_HELP   299
  93.  
  94. #define ID_COPY   510
  95. #define ID_CUT    511
  96. #define ID_PASTE  512
  97. #define ID_CLEAR  513
  98.  
  99. #define ID_GETLEN    520
  100. #define ID_GETLINES  521
  101. #define ID_GETHANDLE 522
  102.  
  103. #define IDM_MEMAVAIL  600
  104.  
  105. #define IDS_MAINTITLE 500
  106.  
  107. /*
  108.   Function declarations
  109. */
  110. extern  int      PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
  111.  
  112. extern  LONG FAR PASCAL MainWndProc(HWND,WORD,WORD,LONG);
  113. extern  LONG FAR PASCAL MsgWndProc(HWND,WORD,WORD,LONG);
  114. extern  LONG FAR PASCAL NotepadWndProc(HWND,WORD,WORD,LONG);
  115. extern  int  FAR PASCAL ListboxDialogProc(HWND,WORD,WORD,DWORD);
  116. extern  int  FAR PASCAL IntervalDlgProc(HWND,WORD,WORD,DWORD);
  117. extern  LONG FAR PASCAL TimerWndProc(HWND,WORD,WORD,LONG);
  118. extern  int  FAR PASCAL ComboDialogProc(HWND,WORD,WORD,DWORD);
  119. extern  LONG FAR PASCAL PanelWndProc(HWND,WORD,WORD,LONG);
  120. extern  LONG FAR PASCAL CalcDlgProc(HWND,WORD,WORD,LONG);
  121. extern  int  FAR PASCAL DlgProc(HWND,WORD,WORD,DWORD);
  122. extern  int  FAR PASCAL IntervalDlgProc(HWND,WORD,WORD,DWORD);
  123.  
  124. extern  BOOL InitApp(HANDLE);
  125. extern  void TimerDemo(void);
  126. extern  HMENU TimerMenuInit(void);
  127. extern  void NewInterval(HWND);
  128. extern  int AreTimersRunning(void);
  129. extern  void KillAllTimers(void);
  130. extern  int MouseDemo(void );
  131. extern  int PrintScrollbarValues(HWND,WORD,WORD,LONG);
  132. extern  void DoFloatingPopup(void);
  133. extern  void ComboDemo(void);
  134. extern  int NotePad(void );
  135. extern  int NotepadOpenFile(LPSTR);
  136. extern  void DOSEscape(void );
  137. extern  int Calculator(void );
  138. extern  void ShowNumber(HWND,DWORD);
  139. extern  DWORD CalcIt(DWORD,WORD,DWORD);
  140. extern  int BadCommand(void );
  141. extern  void ChildDemo(void );
  142. extern  void CreateChildren(HWND);
  143. extern  void TerminateChildDemo(void );
  144. extern  void GraphicsModeDemo(void );
  145. extern  void VideoModeDemo(void );
  146. extern  LONG memavail(void );
  147.  
  148. #ifdef PROPVIEW
  149. extern int PASCAL PVWinMain(HANDLE, HANDLE, LPSTR, int);
  150. extern HWND hDlgActive;
  151. #endif
  152.  
  153.  
  154. /*
  155.   Global Window Handles
  156. */
  157. HWND hMain;
  158. HWND hMsgWnd;
  159. HWND hTimeWnd;
  160. HWND hMouseWnd = NULL;
  161. HWND hWndCalc = NULL;
  162.  
  163. #ifdef MODELESSTEST
  164. HWND hWndListbox = NULL;
  165. HWND hWndString;
  166. #endif
  167.  
  168. int cxMsgWnd, cyMsgWnd;
  169. int cxTimeWnd;
  170.  
  171.  
  172. HANDLE hAppInstance = 0;
  173.  
  174. #define MM_SHOWTIME    (WM_USER + 243)
  175.  
  176.  
  177.  
  178. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  179.   HANDLE hInstance, hPrevInstance;
  180.   LPSTR  lpszCmdLine;
  181.   int    nCmdShow;
  182. {
  183.   MSG    msg;
  184.   char   szBuf[64];
  185.   HANDLE hAccel;
  186.   RECT   r;
  187.   HDC    hDC;
  188.   TEXTMETRIC tm;
  189.   static DWORD dwLastTick = 0L;
  190.  
  191.  
  192.   (void) lpszCmdLine;
  193.  
  194.   VidSetBlinking(FALSE);
  195.  
  196.   /*
  197.     Register the main window class.
  198.   */
  199.   if (!hPrevInstance)
  200.     if (!InitApp(hInstance))
  201.       exit(1);
  202.  
  203.   /*
  204.     Create the main window and set the main window procedure
  205.   */
  206.   LoadString(hInstance, IDS_MAINTITLE, (LPSTR) szBuf, sizeof(szBuf));
  207.   hMain = CreateWindow("Mewldemo",            /* class */
  208.                        szBuf,                 /* title */
  209.                        WS_OVERLAPPEDWINDOW |  /* dwStyle */ 
  210.                        WS_VSCROLL | WS_HSCROLL,
  211.                        CW_USEDEFAULT,0,       /* x,y,width,height */
  212.                        CW_USEDEFAULT,0,       /* x,y,width,height */
  213.                        NULL,                  /* hParent */
  214.                        NULL,                  /* hMenu */
  215.                        hInstance,             /* hInst */
  216.                        0L);                   /* lpParam */
  217.  
  218.   /*
  219.     Load in the accelerator table
  220.   */
  221.   hAccel = LoadAccelerators(hInstance, (LPSTR) "Mewldemo");
  222.  
  223.  
  224.   /*
  225.     Get coordinate info to help us set up the message and time windows
  226.   */
  227.   hDC = GetDC(hMain);
  228.   GetTextMetrics(hDC, (LPTEXTMETRIC) &tm);
  229.   GetClientRect(hMain, (LPRECT) &r);
  230.   ReleaseDC(hMain, hDC);
  231.  
  232.   cyMsgWnd = (tm.tmHeight+tm.tmExternalLeading) +
  233.                        (2*GetSystemMetrics(SM_CYBORDER)),
  234.   cxMsgWnd  = 42 * tm.tmAveCharWidth + (2*GetSystemMetrics(SM_CXBORDER));
  235.   cxTimeWnd = LOWORD(GetTextExtent(hDC,"55:55:55",8)) +
  236.                   (2*GetSystemMetrics(SM_CXBORDER));
  237.  
  238.   /*
  239.     Create a child window to display the output messages.
  240.   */
  241.   hMsgWnd =  CreateWindow("MsgWnd",        /* class */
  242.                NULL,           /* title */
  243.                WS_CHILD | WS_BORDER | WS_VISIBLE,       /* dwStyle */
  244.                0,
  245.                r.bottom - cyMsgWnd + 1,
  246.                            42 * tm.tmAveCharWidth + (2*GetSystemMetrics(SM_CXBORDER)),
  247.                cyMsgWnd,
  248.                hMain,          /* hParent */
  249.                NULL,           /* hMenu */
  250.                hInstance,      /* hInst */
  251.                0L);            /* lpParam */
  252.  
  253.   /*
  254.     Create a window for the clock
  255.   */
  256.   hTimeWnd = CreateWindow("MsgWnd",
  257.               NULL,
  258.               WS_CHILD | WS_BORDER | WS_VISIBLE,
  259.               r.right,
  260.               r.bottom - cyMsgWnd + 1,
  261.               8 * tm.tmAveCharWidth + (2*GetSystemMetrics(SM_CXBORDER)),
  262.               cyMsgWnd,
  263.               hMain,          /* hParent */
  264.               NULL,           /* hMenu */
  265.               hInstance,      /* hInst */
  266.               0L);            /* lpParam */
  267.  
  268.  
  269.   /*
  270.     Show the window
  271.   */
  272.   ShowWindow(hMain, nCmdShow);
  273.   UpdateWindow(hMain);
  274.  
  275. #if defined(PROPVIEW)
  276.   if (!PVWinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow))
  277.     return FALSE;
  278. #endif
  279.  
  280.  
  281.   /*
  282.     Main message loop
  283.   */
  284.   for (;;)
  285.   {
  286.     /*
  287.       This is how you can do background processing
  288.     */
  289.     while (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  290.     {
  291.       DWORD dwTick;
  292.  
  293.       dwTick = GetTickCount();
  294.       if (dwTick >= dwLastTick + 1000)
  295.       {
  296.         SendMessage(hTimeWnd, MM_SHOWTIME, 0, dwTick);
  297.         dwLastTick = dwTick;
  298.       }
  299.     }
  300.  
  301.     if (msg.message == WM_QUIT)
  302.       break;
  303.  
  304.     if (msg.message == WM_MOUSEMOVE && hMouseWnd)
  305.     {
  306.       POINT pt;
  307.  
  308.       pt = MAKEPOINT(msg.lParam);
  309.       wsprintf(szBuf, "x=%3d, y=%3d", pt.y, pt.x);
  310.  
  311.       hDC = GetDC(hMouseWnd);
  312.  
  313.       GetClientRect(hMouseWnd, (LPRECT) &r);
  314.       FillRect(hDC, (LPRECT) &r, GetStockObject(LTGRAY_BRUSH));
  315.       TextOut(hDC, 0, 0, (LPSTR) szBuf, lstrlen(szBuf));
  316.  
  317.       ReleaseDC(hMouseWnd, hDC);
  318.     }
  319.  
  320.     if (hWndCalc && IsDialogMessage(hWndCalc, &msg))
  321.       continue;
  322.  
  323. #ifdef MODELESSTEST
  324.     if (hWndListbox && IsDialogMessage(hWndListbox, &msg))
  325.       continue;
  326. #endif
  327.  
  328. #ifdef PROPVIEW
  329.     if(hDlgActive && IsDialogMessage(hDlgActive, (LPMSG) &msg))
  330.       continue;
  331. #endif
  332.  
  333.     if (!TranslateAccelerator(hMain, hAccel, &msg))
  334.     {
  335.       TranslateMessage(&msg);
  336.       DispatchMessage(&msg);
  337.     }
  338.   }
  339.  
  340.   return 0;
  341. }
  342.  
  343.  
  344. /****************************************************************************/
  345. /*                                                                          */
  346. /* Function : InitApp                                                       */
  347. /*                                                                          */
  348. /* Purpose  : Registers the window class                                    */
  349. /*                                                                          */
  350. /* Returns  :                                                               */
  351. /*                                                                          */
  352. /****************************************************************************/
  353. BOOL InitApp(hInstance)
  354.   HANDLE hInstance;
  355. {
  356.   HANDLE    hClass;
  357.   PWNDCLASS pClass;
  358.  
  359.   hClass = LocalAlloc(LPTR | LMEM_ZEROINIT, sizeof(WNDCLASS));
  360.   pClass = (PWNDCLASS) LocalLock(hClass);
  361.  
  362.   pClass->hCursor       = LoadCursor(NULL, IDC_ARROW);
  363.   pClass->hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  364.   pClass->lpszMenuName  = "MainMenu";
  365.   pClass->lpszClassName = "Mewldemo";
  366.   pClass->hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  367.   pClass->hInstance     = hInstance;
  368.   pClass->style         = CS_HREDRAW | CS_VREDRAW;
  369.   pClass->lpfnWndProc   = MainWndProc;
  370.   if (!RegisterClass((LPWNDCLASS) pClass))
  371.     return FALSE;
  372.  
  373.   pClass->lpszMenuName  = (LPSTR) NULL;
  374.   pClass->lpszClassName = "MsgWnd";
  375.   pClass->hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
  376.   pClass->lpfnWndProc   = MsgWndProc;
  377.   if (!RegisterClass((LPWNDCLASS) pClass))
  378.     return FALSE;
  379.  
  380.   /*
  381.     Register the notepad class
  382.   */
  383.   pClass->lpszMenuName  = (LPSTR) "NotePad";
  384.   pClass->lpszClassName = "NotePad";
  385.   pClass->lpfnWndProc   = NotepadWndProc;
  386.   if (!RegisterClass((LPWNDCLASS) pClass))
  387.     return FALSE;
  388.  
  389.   /*
  390.     Register the timer window
  391.   */
  392.   pClass->lpszMenuName  = (LPSTR) NULL;
  393.   pClass->lpszClassName = "Timer";
  394.   pClass->lpfnWndProc   = TimerWndProc;
  395.   pClass->hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
  396.   if (!RegisterClass((LPWNDCLASS) pClass))
  397.     return FALSE;
  398.  
  399.   /*
  400.     Register the calculator window
  401.   */
  402.   pClass->lpszClassName = "HexCalc";
  403.   pClass->lpfnWndProc   = CalcDlgProc;
  404.   pClass->hbrBackground = (HBRUSH) COLOR_WINDOW + 1;
  405.   pClass->cbWndExtra    = DLGWINDOWEXTRA;
  406.   if (!RegisterClass((LPWNDCLASS) pClass))
  407.     return FALSE;
  408.  
  409.  
  410.   LocalUnlock(hClass);
  411.   LocalFree(hClass);
  412.   hAppInstance = hInstance;
  413.   return TRUE;            /* Initialization succeeded */
  414. }
  415.  
  416.  
  417. /****************************************************************************/
  418. /*                                                                          */
  419. /* Function : MainWndProc                                                   */
  420. /*                                                                          */
  421. /* Purpose  : Window procedure for the main window.                         */
  422. /*                                                                          */
  423. /* Returns  :                                                               */
  424. /*                                                                          */
  425. /****************************************************************************/
  426. LONG FAR PASCAL MainWndProc(HWND hWnd, WORD message, WORD wParam, LONG lParam)
  427. {
  428.   char    buf[80];
  429.   FARPROC lpfn;
  430.  
  431.   switch (message)
  432.   {
  433.     case WM_COMMAND     :
  434.       switch (wParam)
  435.       {
  436.         case ID_LISTDEMO :
  437.           if (AreTimersRunning())
  438.             KillAllTimers();
  439. #ifdef MODELESSTEST
  440.           if ((hWndListbox = 
  441.                CreateDialog(hInstance,"ListDemoDlg",hMain,
  442.                             (FARPROC) ListboxDialogProc)) != NULL)
  443.           {
  444.             ShowWindow(hWndListbox, SW_SHOW);
  445.             SetFocus(hWndListbox);
  446.             hWndString = GetDlgItem(hWndListbox, ID_STRING);
  447.           }
  448. #else
  449.           lpfn = MakeProcInstance((FARPROC) ListboxDialogProc, hAppInstance);
  450.           DialogBox(hAppInstance, "ListDemoDlg", hMain, (FARPROC) lpfn);
  451.           FreeProcInstance(lpfn);
  452. #endif
  453.           break;
  454.  
  455.  
  456.         case ID_CHILDDEMO:
  457.           ChildDemo();
  458.           break;
  459.  
  460.         case ID_CALCDEMO :
  461.           Calculator();
  462.           break;
  463.  
  464.         case ID_TIMEDEMO :
  465.           if (AreTimersRunning())
  466.             KillAllTimers();
  467.           TimerDemo();
  468.           break;
  469.  
  470.         case ID_MOUSEDEMO:
  471.           if (hMouseWnd)
  472.           {
  473.             DestroyWindow(hMouseWnd);
  474.             hMouseWnd = NULL;
  475.           }
  476.           else
  477.           {
  478.             MouseDemo();
  479.           }
  480.           CheckMenuItem(GetMenu(hMain), ID_MOUSEDEMO,
  481.                         hMouseWnd ? MF_CHECKED : MF_UNCHECKED);
  482.           break;
  483.  
  484.         case ID_MENUDEMO:
  485.           DoFloatingPopup();
  486.           break;
  487.  
  488.         case ID_EDITDEMO :
  489.           NotePad();
  490.           break;
  491.  
  492.         case ID_COMBODEMO:
  493.           ComboDemo();
  494.           break;
  495.  
  496.  
  497.         case ID_WORD50 :
  498.         case ID_WRITE  :
  499.         case ID_EXCEL  :
  500.         case ID_124    :
  501.         case ID_C52    :
  502.         case ID_QUICKC :
  503.         case ID_QUICKBASIC :
  504.         case ID_PASCAL :
  505.           LoadString(hAppInstance, wParam+1000, (LPSTR) buf, sizeof(buf));
  506.           MessageBox(NULL, buf, "WM_COMMAND", MB_OK);
  507.           break;
  508.  
  509.         case ID_ABOUT   :
  510.           MessageBox(NULL,"MEWEL Window System\nMagma Systems\n(201) 912-0192",
  511.                      "About MEWEL", MB_OK | MB_ICONASTERISK);
  512.           break;
  513.  
  514.         case ID_EXIT :
  515.           PostQuitMessage(0);
  516.           break;
  517.  
  518.         case ID_HELP :
  519. dohelp:
  520.       MessageBox(hWnd,
  521. "MEWEL Demonstration Program\nMagma Systems\n15 Bodwell Terrace\nMillburn, NJ 07041\n\
  522. (201) 912-0192 (voice)\n(201) 912-0668 (BBS)\n(201) 912-0103 (FAX)",
  523.                  "MEWLDEMO", MB_OK | MB_ICONASTERISK);
  524.          break;
  525.       }
  526.       break;
  527.  
  528.  
  529.     case WM_VSCROLL :
  530.     case WM_HSCROLL :
  531.       PrintScrollbarValues(hWnd, message, wParam, lParam);
  532.       break;
  533.  
  534.  
  535.     case WM_SIZE :
  536.     {
  537.       RECT r;
  538.       GetClientRect(hWnd, (LPRECT) &r);
  539.       MoveWindow(hMsgWnd, 0, r.bottom - cyMsgWnd+1, cxMsgWnd, cyMsgWnd, TRUE);
  540.       MoveWindow(hTimeWnd, cxMsgWnd, r.bottom - cyMsgWnd+1, cxTimeWnd, cyMsgWnd, TRUE);
  541.       break;
  542.     }
  543.  
  544.     case WM_MENUSELECT  :
  545.     {
  546.       int  iLen;
  547.       RECT r;
  548.       HDC  hDC;
  549.  
  550.       /*
  551.         We trigger off of the WM_MENUSELECT message so that we
  552.         can print informative messages about each menu option.
  553.       */
  554.  
  555.       GetClientRect(hMsgWnd, (LPRECT) &r);
  556.       hDC = GetDC(hMsgWnd);
  557.       FillRect(hDC, (LPRECT) &r, GetStockObject(LTGRAY_BRUSH));
  558.  
  559.       /*
  560.         If lParam was <-1,0>, then the user dismissed the menu, so
  561.         we just want to erase the previous help text from the
  562.         status window. If not, then idMenu contains a valid menu or
  563.         submenu identifier. Load the corresponding string from
  564.         the string table, and print it.
  565.       */
  566.       if (LOWORD(lParam) != 0xFFFF || HIWORD(lParam) != 0)
  567.       {
  568.         if ((iLen = LoadString(hAppInstance, wParam, (LPSTR) buf, sizeof(buf))) > 0)
  569.         {
  570.           TextOut(hDC, 0, 0, buf, iLen);
  571.         }
  572.       }
  573.  
  574.       ReleaseDC(hMsgWnd, hDC);
  575.       break;
  576.     }
  577.  
  578.     /*
  579.       WM_HELP is a MEWEL-specific message which is sent when a
  580.       menu item withthe HELP attribute is clicked on. 
  581.     */
  582.     case WM_HELP :
  583.       goto dohelp;
  584.  
  585.  
  586. #ifdef RAINBOW
  587.     case WM_PAINT :
  588.     {
  589.       PAINTSTRUCT ps;
  590.       HDC         hDC;
  591.       RECT        r;
  592.       HBRUSH      hBr;
  593.       int         i;
  594.  
  595.       hDC = BeginPaint(hWnd, (LPPAINTSTRUCT) &ps);
  596.       GetClientRect(hWnd, (LPRECT) &r);
  597.  
  598.       for (i = 0;  i < 16;  i++)
  599.       {
  600.         r.top = r.bottom = i;
  601.         hBr = CreateSolidBrush(AttrToRGB(i));
  602.         FillRect(hDC, (LPRECT) &r, hBr);
  603.         DeleteObject(hBr);
  604.       }
  605.  
  606.       EndPaint(hWnd, (LPPAINTSTRUCT) &ps);
  607.       break;
  608.     }
  609. #endif
  610.  
  611.  
  612.     default :
  613.       /* Call the default window procedure for the main window */
  614.       return DefWindowProc(hWnd, message, wParam, lParam);
  615.   }
  616.   return TRUE;
  617. }
  618.  
  619.  
  620. LONG FAR PASCAL MsgWndProc(HWND hWnd, WORD message, WORD wParam, LONG lParam)
  621. {
  622.   switch (message)
  623.   {
  624.     case MM_SHOWTIME :
  625.     {
  626.       char szBuf[32];
  627.       HDC  hDC;
  628.  
  629.       lParam /= 1000L;  /* convert millisecs to seconds */
  630.       wsprintf(szBuf, "%02d:%02d:%02d", 
  631.                  (int) (lParam / 3600L),
  632.                  (int) (lParam % 3600L) / 60,
  633.                  (int) (lParam % 3600L) % 60);
  634.       hDC = GetDC(hWnd);
  635.       TextOut(hDC, 0, 0, (LPSTR) szBuf, lstrlen(szBuf));
  636.       ReleaseDC(hWnd, hDC);
  637.       return TRUE;
  638.     }
  639.  
  640.     default :
  641.       return DefWindowProc(hWnd, message, wParam, lParam);
  642.   }
  643. }
  644.  
  645.  
  646.  
  647.  
  648. /*==========================================================================*/
  649. /*                                                                          */
  650. /* Function : ListDemo()                                                    */
  651. /*                                                                          */
  652. /* Purpose  : Code to implement the listbox demo                            */
  653. /*                                                                          */
  654. /* Returns  :                                                               */
  655. /*                                                                          */
  656. /*==========================================================================*/
  657. int FAR PASCAL ListboxDialogProc(HWND hDlg, WORD message, WORD wParam, DWORD lParam)
  658. {
  659.   char szString[130];
  660.   int  iPos;
  661.   int  operation;
  662.   int  iSel;
  663.   static BYTE szItemText[130];
  664.   static int  nMovedItem = - 1;
  665.  
  666. #ifdef MODELESSTEST
  667.   static HBRUSH hBlueBrush = 0;
  668. #endif
  669.  
  670.   switch (message)
  671.   {
  672.     case WM_INITDIALOG :
  673.       CheckRadioButton(hDlg, ID_APPEND, ID_DELETE, ID_APPEND);
  674. #ifdef MODELESSTEST
  675.       if (!hBlueBrush)
  676.       {
  677.         hBlueBrush = CreateSolidBrush(RGB(0, 0, 255));
  678.         SetWindowsCompatibility(WC_SEND_WMCTLCOLOR);
  679.       }
  680. #endif
  681.       return TRUE;
  682.  
  683.     case WM_COMMAND    :
  684.       switch (wParam)
  685.       {
  686.         case ID_INSERT :
  687.         case ID_DELETE :
  688.         case ID_APPEND :
  689.           CheckRadioButton(hDlg, ID_APPEND, ID_DELETE, wParam);
  690.           break;
  691.  
  692.           case IDOK      :
  693.           /*
  694.             The user pressed the OK button. We want to get the string
  695.             that is to be inserted as well as the position.
  696.           */
  697.           GetDlgItemText(hDlg, ID_STRING, (LPSTR) szString, sizeof(szString));
  698.           iPos = GetDlgItemInt(hDlg, ID_POS, NULL, TRUE);
  699.  
  700.           /*
  701.             Determine the operation to perform
  702.           */
  703.           operation = 0;
  704.           if (IsDlgButtonChecked(hDlg, ID_APPEND))
  705.             operation = LB_ADDSTRING;
  706.           else if (IsDlgButtonChecked(hDlg, ID_INSERT))
  707.             operation = LB_INSERTSTRING;
  708.           else if (IsDlgButtonChecked(hDlg, ID_DELETE))
  709.             operation = LB_DELETESTRING;
  710.  
  711.           if (operation)
  712.           {
  713.             SendDlgItemMessage(hDlg, ID_LISTBOX, operation, iPos,
  714.                                       (DWORD) (LPSTR) szString);
  715.             MessageBox(hMain, szString, "Completed", MB_OK | MB_ICONEXCLAMATION);
  716.           }
  717.           break;
  718.  
  719.         case IDCANCEL  :
  720.           EndDialog(hDlg, 0);
  721.           break;
  722.  
  723.         case ID_LISTBOX :
  724.           switch (HIWORD(lParam))
  725.           {
  726.             case LBN_SELCHANGE :
  727.               if (nMovedItem < 0)
  728.                 return FALSE;
  729.               iSel = (int) SendDlgItemMessage(hDlg,ID_LISTBOX,LB_GETCURSEL,0,0L);
  730.               SendDlgItemMessage(hDlg,ID_LISTBOX,LB_DELETESTRING,nMovedItem,0L);
  731.               SendDlgItemMessage(hDlg,ID_LISTBOX,LB_INSERTSTRING,iSel,
  732.                                  (DWORD) (BYTE far *) szItemText);
  733.               nMovedItem = iSel;
  734.               break;
  735.           }
  736.           return FALSE;
  737.       }
  738.  
  739.       return TRUE;
  740.  
  741. #ifdef MODELESSTEST
  742.     case WM_SETFOCUS :
  743.       SetFocus(GetDlgItem(hDlg, ID_STRING));
  744.       return TRUE;
  745.  
  746.     case WM_CTLCOLOR :
  747.     if (HIWORD(lParam) == CTLCOLOR_EDIT)
  748.     {
  749.       HBRUSH hBrush = hBlueBrush;
  750.       SetTextColor(wParam, RGB(0, 255, 255));   // cyan fore
  751.       SetBkColor(wParam, RGB(0, 0, 255));  // blue background
  752.       return hBrush;
  753.     }
  754.     else
  755.       return GetStockObject(WHITE_BRUSH);
  756.  
  757.  
  758.     case WM_CLOSE :
  759.       hWndListbox = NULL;
  760.       DestroyWindow(hDlg);
  761.       return TRUE;
  762. #endif
  763.  
  764.   }
  765.  
  766.   return FALSE;
  767. }
  768.  
  769.  
  770. /*===========================================================================*/
  771. /*                                                                           */
  772. /* File    :  MOVETEST.C                                                     */
  773. /*                                                                           */
  774. /* Purpose :  Demo of child windows. Included moving, resizing, and clipping */
  775. /*                                                                           */
  776. /* History :                                                                 */
  777. /*                                                                           */
  778. /* (C) Copyright 1989 Marc Adler/Magma Systems     All Rights Reserved       */
  779. /*===========================================================================*/
  780. extern LONG FAR PASCAL PanelWndProc(HWND, WORD, WORD, LONG);
  781. HWND hPanel1, hPanel2, hPanel3;
  782.  
  783. static PSTR pszChildMsg = (PSTR) 
  784. "You can move (drag the caption) or resize (drag lower-right corner) \
  785. any of the 3 child windows.\n You can press the arrow keys to scroll \
  786. the panels. This also demonstrates MEWEL clipping sibling windows.";
  787.  
  788.  
  789. VOID ChildDemo(VOID)
  790. {
  791.   HANDLE hMemory;
  792.   PWNDCLASS pWndClass;
  793.  
  794.   TerminateChildDemo();
  795.   MessageBox(hMain, pszChildMsg, "Child Demo", MB_OK);
  796.  
  797.   hMemory = LocalAlloc(LPTR | LMEM_ZEROINIT, sizeof(WNDCLASS));
  798.   pWndClass = (PWNDCLASS) LocalLock(hMemory);
  799.   pWndClass->hCursor       = LoadCursor(NULL, IDC_ARROW);
  800.   pWndClass->hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  801.   pWndClass->lpszMenuName  = NULL;
  802.   pWndClass->lpszClassName = "Panel";
  803.   pWndClass->lpfnWndProc   = PanelWndProc;
  804.   pWndClass->cbWndExtra    = sizeof(HBRUSH);
  805.  
  806.   RegisterClass(pWndClass);
  807.  
  808.   LocalUnlock(hMemory);
  809.   LocalFree(hMemory);
  810.  
  811.  
  812.   hPanel1=CreateWindow("Panel",        /* class */
  813.                        "Panel1",       /* title */
  814.                        WS_BORDER  | WS_CHILD |
  815.                        WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_MAXIMIZEBOX |
  816.                        WS_VSCROLL | WS_HSCROLL | WS_THICKFRAME |
  817.                        WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
  818.                        MAKE_X(4), MAKE_Y(3),
  819.                        MAKE_X(30), MAKE_Y(14),   /* x,y,width,height */
  820.                        hMain,          /* hParent */
  821.                        1,              /* id */
  822.                        hAppInstance,   /* hInst */
  823.                        0L);            /* lpParam */
  824.   SetWindowWord(hPanel1, 0, CreateSolidBrush(RGB(255, 0, 0)));
  825.   CreateChildren(hPanel1);
  826.  
  827.   hPanel2=CreateWindow("Panel",        /* class */
  828.                        "Panel2",       /* title */
  829.                        WS_BORDER  | WS_CHILD |
  830.                        WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_MAXIMIZEBOX |
  831.                        WS_VSCROLL | WS_HSCROLL | WS_THICKFRAME |
  832.                        WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
  833.                        MAKE_X(24), MAKE_Y(6),
  834.                        MAKE_X(30), MAKE_Y(14),   /* x,y,width,height */
  835.                        hMain,          /* hParent */
  836.                        2,              /* id */
  837.                        hAppInstance,   /* hInst */
  838.                        0L);            /* lpParam */
  839.   SetWindowWord(hPanel2, 0, CreateSolidBrush(RGB(255, 0, 255)));
  840.   CreateChildren(hPanel2);
  841.  
  842.   hPanel3=CreateWindow("Panel",        /* class */
  843.                        "Panel3",       /* title */
  844.                        WS_BORDER  | WS_CHILD |
  845.                        WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_MAXIMIZEBOX |
  846.                        WS_VSCROLL | WS_HSCROLL | WS_THICKFRAME |
  847.                        WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
  848.                        MAKE_X(44), MAKE_Y(9),
  849.                        MAKE_X(30), MAKE_Y(14),   /* x,y,width,height */
  850.                        hMain,          /* hParent */
  851.                        3,              /* id */
  852.                        hAppInstance,   /* hInst */
  853.                        0L);            /* lpParam */
  854.   SetWindowWord(hPanel3, 0, CreateSolidBrush(RGB(0, 0, 255)));
  855.   CreateChildren(hPanel3);
  856.  
  857.   /*
  858.     Show the window
  859.   */
  860.   ShowWindow(hPanel1, SW_SHOW);
  861.   ShowWindow(hPanel2, SW_SHOW);
  862.   ShowWindow(hPanel3, SW_SHOW);
  863.   BringWindowToTop(hPanel1);
  864.   SetFocus(hPanel1);
  865.   CheckMenuItem(GetMenu(hMain), ID_CHILDDEMO, MF_CHECKED);
  866. }
  867.  
  868.  
  869. #ifdef MEWEL
  870. #define SCROLL_AMT 1
  871. #else
  872. #define SCROLL_AMT 16
  873. #endif
  874.  
  875. LONG FAR PASCAL PanelWndProc(HWND hWnd, WORD message, WORD wParam, LONG lParam)
  876. {
  877.   switch (message)
  878.   {
  879. #if 0
  880.     case WM_MOUSEACTIVATE :
  881.       BringWindowToTop(hWnd);
  882.       return MA_ACTIVATEANDEAT;
  883. #endif
  884.  
  885.     case WM_KEYDOWN       :
  886.       switch (wParam)
  887.       {
  888.         case VK_DOWN:
  889.           ScrollWindow(hWnd, 0,  SCROLL_AMT,  (LPRECT) NULL, (LPRECT) NULL);
  890.           break;
  891.         case VK_UP  :
  892.           ScrollWindow(hWnd, 0, -SCROLL_AMT,  (LPRECT) NULL, (LPRECT) NULL);
  893.           break;
  894.         case VK_LEFT:
  895.           ScrollWindow(hWnd, -SCROLL_AMT, 0,  (LPRECT) NULL, (LPRECT) NULL);
  896.           break;
  897.         case VK_RIGHT:
  898.           ScrollWindow(hWnd, SCROLL_AMT, 0,   (LPRECT) NULL, (LPRECT) NULL);
  899.           break;
  900.         case VK_HOME:
  901.           ScrollWindow(hWnd, -SCROLL_AMT, -SCROLL_AMT, (LPRECT) NULL, (LPRECT) NULL);
  902.           break;
  903.         case VK_PRIOR:
  904.           ScrollWindow(hWnd,  SCROLL_AMT, -SCROLL_AMT, (LPRECT) NULL, (LPRECT) NULL);
  905.           break;
  906.         case VK_END :
  907.           ScrollWindow(hWnd, -SCROLL_AMT,  SCROLL_AMT, (LPRECT) NULL, (LPRECT) NULL);
  908.           break;
  909.         case VK_NEXT:
  910.           ScrollWindow(hWnd,  SCROLL_AMT,  SCROLL_AMT, (LPRECT) NULL, (LPRECT) NULL);
  911.           break;
  912.       }
  913.       return TRUE;
  914.  
  915.     case WM_CLOSE :
  916.       if (hWnd == hPanel1)
  917.         hPanel1 = NULL;
  918.       else
  919.       if (hWnd == hPanel2)
  920.         hPanel2 = NULL;
  921.       else
  922.       if (hWnd == hPanel3)
  923.         hPanel3 = NULL;
  924.       DeleteObject((HBRUSH) GetWindowWord(hWnd, 0));
  925.       DestroyWindow(hWnd);
  926.       if (!hPanel1 && !hPanel2 && !hPanel3)
  927.         CheckMenuItem(GetMenu(hMain), ID_CHILDDEMO, MF_UNCHECKED);
  928.       break;
  929.  
  930.     case WM_ERASEBKGND :
  931.     {
  932.       RECT r;
  933.       GetClientRect(hWnd, (LPRECT) &r);
  934.       FillRect((HDC) wParam, (LPRECT) &r, (HBRUSH) GetWindowWord(hWnd, 0));
  935.       return TRUE;
  936.     }
  937.  
  938.     case WM_CTLCOLOR :
  939.       if (HIWORD(lParam) == CTLCOLOR_EDIT || HIWORD(lParam) == CTLCOLOR_STATIC)
  940.       {
  941.         HBRUSH   hBr = (HBRUSH) GetWindowWord(hWnd, 0);
  942.         LOGBRUSH lb;
  943.  
  944.         GetObject(hBr, sizeof(lb), (LPSTR) &lb);
  945.         SetBkColor((HDC) wParam, lb.lbColor);
  946.         return hBr;
  947.       }
  948.       else
  949.         return DefWindowProc(hWnd, message, wParam, lParam);
  950.  
  951.     default :
  952.       /* Call the default window procedure for the main window */
  953.       return DefWindowProc(hWnd, message, wParam, lParam);
  954.   }
  955.  
  956.   return FALSE;
  957. }
  958.  
  959.  
  960. struct childinfo
  961. {
  962.   char *szClass;
  963.   char *szTitle;
  964.   int  x, y, cx, cy;
  965.   int  id;
  966. } ChildInfo[] =
  967. {
  968.   { "Static",  "Business1:",1, 0,15,1, 24 } ,
  969.   { "Edit",  NULL,       20, 0,45,1, 25 },
  970.   { "Static",  "Business2:",1, 2,15,1, 24 } ,
  971.   { "Edit",  NULL,       20, 2,45,1, 25 },
  972.   { "Static",  "Business3", 1, 4,15,1, 24 } ,
  973.   { "Edit",  NULL,       20, 4,45,1, 25 },
  974.   { "Static",  "Business4", 1, 6,15,1, 24 } ,
  975.   { "Edit",  NULL,       20, 6,45,1, 25 } ,
  976.   { "Static",  "Business5", 1, 8,15,1, 24 } ,
  977.   { "Edit",  NULL,       20, 8,45,1, 25 },
  978. };
  979.  
  980.  
  981. VOID CreateChildren(HWND hParent)
  982. {
  983.   struct childinfo *pCI;
  984.   int    i;
  985.   DWORD dwStyle;
  986.   RECT  r;
  987.  
  988.   GetClientRect(hParent, &r);
  989.  
  990.   for (i = 0;  i < sizeof(ChildInfo)/sizeof(ChildInfo[0]);  i++)
  991.   {
  992.     pCI = &ChildInfo[i];
  993.     if (i & 0x01)
  994.     {
  995.       dwStyle = ES_LEFT | WS_BORDER;
  996.     }
  997.     else
  998.     {
  999.       dwStyle = SS_LEFT;
  1000.     }
  1001.  
  1002.     CreateWindow(pCI->szClass, 
  1003.                  pCI->szTitle, 
  1004.                  dwStyle | WS_VISIBLE | WS_CHILD,
  1005.                  MAKE_X(pCI->x), 
  1006.                  MAKE_Y(pCI->y), 
  1007.                  MAKE_X(pCI->cx), 
  1008.                  MAKE_Y(pCI->cy),
  1009.                  hParent,        /* hParent */
  1010.                  pCI->id,        /* hMenu */
  1011.                  hAppInstance,   /* hInst */
  1012.                  0L);            /* lpParam */
  1013.   }
  1014.  
  1015.   CreateWindow("Button", 
  1016.                "Push Me!", 
  1017.                WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  1018.                MAKE_X(1), 
  1019.                (r.bottom-MAKE_Y(1)),
  1020.                MAKE_X(10),
  1021.                MAKE_Y(1),
  1022.                hParent,        /* hParent */
  1023.                100,            /* hMenu */
  1024.                hAppInstance,   /* hInst */
  1025.                0L);            /* lpParam */
  1026.  
  1027. }
  1028.  
  1029.  
  1030. VOID TerminateChildDemo(VOID)
  1031. {
  1032.   if (hPanel1)
  1033.     SendMessage(hPanel1, WM_CLOSE, 0, 0L);
  1034.   if (hPanel2)
  1035.     SendMessage(hPanel2, WM_CLOSE, 0, 0L);
  1036.   if (hPanel3)
  1037.     SendMessage(hPanel3, WM_CLOSE, 0, 0L);
  1038.   CheckMenuItem(GetMenu(hMain), ID_CHILDDEMO, MF_UNCHECKED);
  1039. }
  1040.  
  1041.  
  1042. /*========================================================================*/
  1043. /*                                                                        */
  1044. /*  SCRLDEMO.C - demonstration of the usage of scroll bars....            */
  1045. /*                                                                        */
  1046. /*========================================================================*/
  1047. int PrintScrollbarValues(HWND hWnd, WORD message, WORD wParam, LONG lParam)
  1048. {
  1049.   char *pszMsg;
  1050.   char buf[80];
  1051.   int  nCode;
  1052.   int  minpos, maxpos, currpos;
  1053.   HDC  hDC;
  1054.   HBRUSH hBrush;
  1055.   RECT   r;
  1056.  
  1057.   (void) lParam;
  1058.  
  1059.   nCode = (message == WM_HSCROLL) ? SB_HORZ : SB_VERT;
  1060.   currpos = GetScrollPos(hWnd, nCode);
  1061.   GetScrollRange(hWnd, nCode, &minpos, &maxpos);
  1062.  
  1063.   switch (wParam)
  1064.   {
  1065.     case SB_LINEUP   :
  1066.       SetScrollPos(hWnd, nCode, max(currpos-1, minpos), TRUE);
  1067.       currpos = GetScrollPos(hWnd, nCode);
  1068.       pszMsg = "SB_LINEUP";
  1069.       break;
  1070.  
  1071.     case SB_LINEDOWN :
  1072.       SetScrollPos(hWnd, nCode, min(currpos+1, maxpos), TRUE);
  1073.       currpos = GetScrollPos(hWnd, nCode);
  1074.       pszMsg = "SB_LINEDOWN";
  1075.       break;
  1076.  
  1077.     case SB_PAGEUP   :
  1078.       SetScrollPos(hWnd, nCode, max(currpos-10, minpos), TRUE);
  1079.       currpos = GetScrollPos(hWnd, nCode);
  1080.       pszMsg = "SB_PAGEUP";
  1081.       break;
  1082.  
  1083.     case SB_PAGEDOWN :
  1084.       SetScrollPos(hWnd, nCode, min(currpos+10, maxpos), TRUE);
  1085.       currpos = GetScrollPos(hWnd, nCode);
  1086.       pszMsg = "SB_PAGEDOWN";
  1087.       break;
  1088.  
  1089.     case SB_TOP        :
  1090.       pszMsg = "SB_TOP";
  1091.       goto do_it;
  1092.     case SB_BOTTOM     :
  1093.       pszMsg = "SB_BOTTOM";
  1094.       goto do_it;
  1095.     case SB_THUMBTRACK :
  1096.       pszMsg = "SB_THUMBTRACK";
  1097.       goto do_it;
  1098.     case SB_THUMBPOSITION :
  1099.       pszMsg = "SB_THUMBPOSITION";
  1100.       goto do_it;
  1101.     case SB_ENDSCROLL  :
  1102.       pszMsg = "SB_ENDSCROLL";
  1103. do_it:
  1104.       currpos = GetScrollPos(hWnd, nCode);
  1105.       break;
  1106.   }
  1107.  
  1108.   wsprintf(buf, "Msg [%s]   Pos [%3d]  %3d%%     ",
  1109.                        (LPSTR) pszMsg, currpos, currpos * 100 / maxpos);
  1110.  
  1111.   hDC = GetDC(hMsgWnd);
  1112.  
  1113.   hBrush = CreateSolidBrush(RGB(0, 128, 128));
  1114.   GetClientRect(hMsgWnd, (LPRECT) &r);
  1115.   FillRect(hMsgWnd, (LPRECT) &r, hBrush);
  1116.   DeleteObject(hBrush);
  1117.  
  1118.   SetTextColor(hDC, RGB(0, 0, 128));
  1119.   SetBkColor(hDC, RGB(0, 128, 128));
  1120.   TextOut(hDC, 0, 0, (LPSTR) buf, lstrlen(buf));
  1121.  
  1122.   ReleaseDC(hMsgWnd, hDC);
  1123.   return TRUE;
  1124. }
  1125.  
  1126.  
  1127. /**************************************************************************/
  1128. /*                                                                        */
  1129. /*             TIMER DEMO                                                 */
  1130. /*                                                                        */
  1131. /**************************************************************************/
  1132. #define NUMTIMERS  4
  1133. HWND hTimerWnd[NUMTIMERS];
  1134.  
  1135. extern LONG FAR PASCAL TimerWndProc(HWND, WORD, WORD, LONG);
  1136.  
  1137.  
  1138. VOID TimerDemo(VOID)
  1139. {
  1140.   int   i;
  1141.   HWND  hWnd;
  1142.   HMENU hMenu;
  1143.   char  title[16];
  1144.   HDC   hDC;
  1145.   TEXTMETRIC tm;
  1146.   int   cx, cy;
  1147.  
  1148.  
  1149.   MessageBox(hMain,
  1150.        "The timer strings will occasionally overwrite the child windows.\nTHIS IS NOT A BUG.",
  1151.        "Heed this", MB_OK | MB_ICONEXCLAMATION);
  1152.  
  1153.   hDC = GetDC(hMain);
  1154.   GetTextMetrics(hDC, (LPTEXTMETRIC) &tm);
  1155.   cx = LOWORD(GetTextExtent(hDC, "555555555555", 12)) +
  1156.                              (2 * GetSystemMetrics(SM_CXBORDER));
  1157.   cy = (tm.tmHeight + tm.tmExternalLeading) +
  1158.                              GetSystemMetrics(SM_CYCAPTION) +
  1159.                              GetSystemMetrics(SM_CYMENU)    +
  1160.                              (2 * GetSystemMetrics(SM_CYBORDER));
  1161.   ReleaseDC(hMain, hDC);
  1162.  
  1163.  
  1164.   for (i = 1;  i <= NUMTIMERS;  i++)
  1165.   {
  1166.     wsprintf(title, "Timer %d", i);
  1167.  
  1168.     hWnd = hTimerWnd[i-1] =
  1169.        CreateWindow("Timer",        /* class */
  1170.                     title,          /* title */
  1171.                     WS_CAPTION | WS_BORDER | WS_CLIPCHILDREN | 
  1172.                     WS_CLIPSIBLINGS | WS_VISIBLE,
  1173.                     ((i-1) % 2) ? MAKE_X(50) : MAKE_X(20), 
  1174.                     MAKE_Y((((i-1) / 2) + 1) * 6),
  1175.                     cx, cy,
  1176.                     hMain,          /* hParent */
  1177.                     NULL,           /* hMenu */
  1178.                     hAppInstance,   /* hInst */
  1179.                     0L);            /* lpParam */
  1180.  
  1181.     hMenu = TimerMenuInit();
  1182.     SetMenu(hWnd, hMenu);
  1183.     DrawMenuBar(hWnd);
  1184.     SetTimer(hWnd, i, i*1000, (FARPROC) NULL);
  1185.   }
  1186.  
  1187.   ShowWindow(hMain, SW_SHOW);
  1188.   SetFocus(hTimerWnd[0]);
  1189. }
  1190.  
  1191.  
  1192. /*
  1193.   This function tests the CreateMenu and ChangeMenu functions. We could
  1194.   have loaded the menu fromt he resource file, but this shows how to
  1195.   create a menu dynamically.
  1196. */
  1197. HMENU TimerMenuInit()
  1198. {
  1199.   HMENU hMenu, hPop;
  1200.  
  1201.   /*
  1202.     Create the menu bar
  1203.   */
  1204.   hMenu = CreateMenu();
  1205.   
  1206.   /*
  1207.     Create a pulldown
  1208.   */
  1209.   hPop = CreateMenu();
  1210.   ChangeMenu(hPop, 0, "&Stop",        ID_STOP,        MF_APPEND);
  1211.   ChangeMenu(hPop, 0, "&Interval",    ID_INTERVAL,    MF_APPEND);
  1212.   ChangeMenu(hPop, 0, "E&xit",        ID_EXIT,        MF_APPEND);
  1213.  
  1214.   /*
  1215.     Attach each of the pulldowns to their corresponding menubar entries
  1216.   */
  1217.   ChangeMenu(hMenu, 0, "&Timer",   hPop, MF_POPUP | MF_APPEND);
  1218.  
  1219.   return hMenu;
  1220. }
  1221.  
  1222.  
  1223. LONG FAR PASCAL TimerWndProc(HWND hWnd, WORD msg, WORD wParam, LONG lParam)
  1224. {
  1225.   char szTime[80];
  1226.   int  i;
  1227.   HDC  hDC;
  1228.  
  1229.   switch (msg)
  1230.   {
  1231.     case WM_TIMER :
  1232.       wsprintf(szTime, "%ld", GetTickCount());
  1233.       hDC = GetDC(hWnd);
  1234.       TextOut(hDC, 0, 0, szTime, lstrlen(szTime));
  1235.       ReleaseDC(hWnd, hDC);
  1236.       MessageBeep(0);
  1237.       return TRUE;
  1238.  
  1239.     case WM_CHAR    :
  1240.       if (wParam == VK_TAB)
  1241.       {
  1242.         for (i = 1;  i <= NUMTIMERS;  i++)
  1243.           if (hTimerWnd[i-1] == hWnd)
  1244.           {
  1245.             SetFocus(hTimerWnd[ (i == NUMTIMERS) ? 0 : i ]);
  1246.             return TRUE;
  1247.           }
  1248.       }
  1249.       break;
  1250.  
  1251.     case WM_SETFOCUS :
  1252.       BringWindowToTop(hWnd);
  1253.       return DefWindowProc(hWnd, msg, wParam, lParam);
  1254.  
  1255.     case WM_COMMAND :
  1256.       switch (wParam)
  1257.       {
  1258.         case ID_EXIT :
  1259.         case ID_STOP :
  1260.           for (i = 1;  i <= NUMTIMERS;  i++)
  1261.             if (hTimerWnd[i-1] == hWnd)
  1262.             {
  1263.               KillTimer(hWnd, i);
  1264.               ModifyMenu(GetMenu(hWnd),
  1265.                          ID_STOP, 
  1266.                          MF_BYCOMMAND | MF_STRING,
  1267.                          ID_START, "Start");
  1268.             }
  1269.  
  1270.           if (wParam == ID_EXIT)
  1271.           {
  1272.             /*
  1273.               Setting focus to a non-timer window will eliminate the
  1274.               situation where DestroyWindow(), detecting that the
  1275.               window with the focus is being destroyed, sets the focus
  1276.               to the next timer window (which will only be destroyed
  1277.               in the next interation of the for loop). This will speed
  1278.               up the process of destruction.
  1279.             */
  1280.             SetFocus(hMain);
  1281.             for (i = 1;  i <= NUMTIMERS;  i++)
  1282.             {
  1283.               DestroyWindow(hTimerWnd[i-1]);
  1284.               hTimerWnd[i-1] = NULL;
  1285.             }
  1286.           }
  1287.           break;
  1288.  
  1289.         case ID_START:
  1290.           for (i = 1;  i <= NUMTIMERS;  i++)
  1291.             if (hTimerWnd[i-1] == hWnd)
  1292.             {
  1293.               SetTimer(hWnd, i, i*1000, (FARPROC) NULL);
  1294.               ModifyMenu(GetMenu(hWnd),
  1295.                          ID_START, 
  1296.                          MF_BYCOMMAND | MF_STRING,
  1297.                          ID_STOP, "Stop");
  1298.             }
  1299.           break;
  1300.  
  1301.         case ID_INTERVAL :
  1302.           NewInterval(hWnd);
  1303.           break;
  1304.       }
  1305.       return TRUE;
  1306.  
  1307.     default :
  1308.       return DefWindowProc(hWnd, msg, wParam, lParam);
  1309.   }
  1310.   return TRUE;
  1311. }
  1312.  
  1313.  
  1314. VOID NewInterval(HWND hWnd)
  1315. {
  1316.   int      i, tid;
  1317.   int      interval;
  1318.   FARPROC  lpfn;
  1319.  
  1320.   for (i = 1;  i <= NUMTIMERS;  i++)
  1321.     if (hTimerWnd[i-1] == hWnd)
  1322.     {
  1323.       tid = i;
  1324.       break;
  1325.     }
  1326.  
  1327.   lpfn = MakeProcInstance(IntervalDlgProc, hAppInstance);
  1328.   if ((interval = DialogBox(hAppInstance, "IntervalDlg", hWnd, lpfn)) != IDCANCEL)
  1329.   {
  1330.     KillTimer(hWnd, tid);
  1331.     SetTimer(hWnd, tid, interval, (FARPROC) NULL);
  1332.   }
  1333.   FreeProcInstance(lpfn);
  1334. }
  1335.  
  1336.  
  1337. int FAR PASCAL IntervalDlgProc(HWND hDlg, WORD msg, WORD wParam, DWORD lParam)
  1338. {
  1339.   char szInterval[80];
  1340.  
  1341.   (void) lParam;
  1342.  
  1343.   switch (msg)
  1344.   {
  1345.     case WM_INITDIALOG :
  1346.       return TRUE;
  1347.  
  1348.     case WM_COMMAND :
  1349.       if (wParam == IDCANCEL)
  1350.         EndDialog(hDlg, IDCANCEL);
  1351.       else if (wParam == IDOK)
  1352.       {
  1353.         GetDlgItemText(hDlg,ID_INTERVAL, (LPSTR)szInterval,sizeof(szInterval));
  1354.         EndDialog(hDlg, atoi(szInterval));
  1355.       }
  1356.       return TRUE;
  1357.  
  1358.     default :
  1359.       return FALSE;
  1360.   }
  1361. }
  1362.  
  1363. int AreTimersRunning(VOID)
  1364. {
  1365.   return hTimerWnd[0];
  1366. }
  1367.  
  1368. VOID KillAllTimers(VOID)
  1369. {
  1370.   SendMessage(hTimerWnd[0], WM_COMMAND, ID_EXIT, 0L);
  1371. }
  1372.  
  1373.  
  1374. /**************************************************************************/
  1375. /*                                                                        */
  1376. /*             MOUSE DEMO                                                 */
  1377. /*                                                                        */
  1378. /**************************************************************************/
  1379. int MouseDemo(VOID)
  1380. {
  1381.   HDC hDC;
  1382.   TEXTMETRIC tm;
  1383.  
  1384.   if (!GetSystemMetrics(SM_MOUSEPRESENT))
  1385.   {
  1386.     MessageBox(NULL, "You must have a mouse to run this demo.",
  1387.                      "Error",MB_OK);
  1388.     return FALSE;
  1389.   }
  1390.  
  1391.   hDC = GetDC(hMain);
  1392.   GetTextMetrics(hDC, (LPTEXTMETRIC) &tm);
  1393.  
  1394.   hMouseWnd = CreateWindow("MsgWnd",       /* class */
  1395.                            "Mouse",        /* title */
  1396.                            WS_BORDER | WS_CHILD,
  1397.                            MAKE_X(30),
  1398.                            MAKE_Y(10),
  1399.                            LOWORD(GetTextExtent(hDC, "x=555, y=555", 12)) +
  1400.                                2*GetSystemMetrics(SM_CXBORDER),
  1401.                            (tm.tmHeight + tm.tmExternalLeading) +
  1402.                                2*GetSystemMetrics(SM_CYBORDER),
  1403.                            hMain,          /* hParent */
  1404.                            NULL,           /* id */
  1405.                            hAppInstance,   /* hInst */
  1406.                            0L);            /* lpParam */
  1407.  
  1408.   ReleaseDC(hMain, hDC);
  1409.   ShowWindow(hMouseWnd, SW_SHOW);
  1410.   UpdateWindow(hMouseWnd);
  1411.   return TRUE;
  1412. }
  1413.  
  1414.  
  1415. /**********************************************************************
  1416. * HEXCALC.C                            
  1417. *                                                  
  1418. * Converted to Mewel by Sal Ricciardi of PC Magazine
  1419. *
  1420. **********************************************************************/
  1421. DWORD CalcIt (DWORD dwFirstNum, WORD nOperation, DWORD dwNum);
  1422. VOID ShowNumber (HWND hWnd, DWORD dwNumber);
  1423.  
  1424. int Calculator(VOID)
  1425. {
  1426.   if (hWndCalc)
  1427.   {
  1428.     BringWindowToTop(hWndCalc);
  1429.     SetFocus(hWndCalc);
  1430.     return TRUE;
  1431.   }
  1432.  
  1433. MessageBox(hMain,
  1434. "The Calculator is a POPUP window, and will stay in front of all other \
  1435. non-popup windows (like the notepad).",
  1436. "Note", MB_OK);
  1437.  
  1438.   if ((hWndCalc = CreateDialog(hAppInstance, "HexCalc", 0, (FARPROC) NULL)) != NULL)
  1439.   {
  1440.     ShowWindow(hWndCalc, SW_SHOW);
  1441.     UpdateWindow(hWndCalc);
  1442.     SetFocus(hWndCalc);
  1443.   }
  1444.   else
  1445.   {
  1446.     MessageBox(hMain, "Could not load calculator", "Error", MB_OK);
  1447.   }
  1448.   return TRUE;
  1449. }
  1450.  
  1451. VOID ShowNumber(HWND hWnd, DWORD dwNumber)
  1452. {
  1453.    char szBuffer[20] ;
  1454.    SetDlgItemText(hWnd, VK_ESCAPE, 
  1455.                     strupr(ltoa(dwNumber, szBuffer, 16))) ;
  1456. }
  1457.  
  1458.  
  1459. DWORD CalcIt(DWORD dwFirstNum, WORD nOperation, DWORD dwNum)
  1460. {
  1461.    switch (nOperation)
  1462.    {
  1463.       case '=' : return dwNum ;
  1464.       case '+' : return dwFirstNum +  dwNum ;
  1465.       case '-' : return dwFirstNum -  dwNum ;
  1466.       case '*' : return dwFirstNum *  dwNum ;
  1467.       case '&' : return dwFirstNum &  dwNum ;
  1468.       case '|' : return dwFirstNum |  dwNum ;
  1469.       case '^' : return dwFirstNum ^  dwNum ;
  1470.       case '<' : return dwFirstNum << dwNum ;
  1471.       case '>' : return dwFirstNum >> dwNum ;
  1472.       case '/' : return dwNum ? dwFirstNum / dwNum : ULONG_MAX ;
  1473.       case '%' : return dwNum ? dwFirstNum % dwNum : ULONG_MAX ;
  1474.       default  : return 0L ;
  1475.    }
  1476. }
  1477.  
  1478. LONG FAR PASCAL CalcDlgProc(HWND hWnd, WORD iMessage, WORD wParam, LONG lParam)
  1479. {
  1480.    static BOOL  bNewNumber = TRUE ;
  1481.    static DWORD dwNumber, dwFirstNum ;
  1482.    static short nOperation = '=' ;
  1483.    HWND         hButton ;
  1484.  
  1485.    (void) lParam;
  1486.  
  1487.    switch (iMessage)
  1488.    {
  1489.      case WM_KEYDOWN:                   /* left arrow --> backspace */
  1490.        if (wParam != VK_LEFT)
  1491.          break ;
  1492.        wParam = VK_BACK ;
  1493.                                              /* fall through */
  1494.      case WM_CHAR:
  1495.        if ((wParam = toupper (wParam)) == VK_RETURN)
  1496.          wParam = '=' ;
  1497.  
  1498.        if ((hButton = GetDlgItem (hWnd, wParam)) != NULL)
  1499.        {
  1500.          SendMessage (hButton, BM_SETSTATE, 1, 0L) ;
  1501.          SendMessage (hButton, BM_SETSTATE, 0, 0L) ;
  1502.        }
  1503.        else
  1504.        {
  1505.          MessageBeep (0) ;
  1506.          break ;
  1507.        }
  1508.                                              /* fall through */
  1509.     case WM_COMMAND:
  1510.      SetFocus (hWnd) ;
  1511.  
  1512.      if (wParam == VK_BACK)                  /* backspace */
  1513.        ShowNumber (hWnd, dwNumber /= 16) ;
  1514.  
  1515.      else if (wParam == VK_ESCAPE)           /* escape */
  1516.        ShowNumber (hWnd, dwNumber = 0L) ;
  1517.  
  1518.      else if (isxdigit (wParam))             /* hex digit */
  1519.      {
  1520.        if (bNewNumber)
  1521.        {
  1522.          dwFirstNum = dwNumber ;
  1523.          dwNumber = 0L ;
  1524.        }
  1525.        bNewNumber = FALSE ;
  1526.  
  1527.        if (dwNumber <= (ULONG_MAX >> 4))
  1528.          ShowNumber (hWnd, dwNumber = 16 * dwNumber + wParam -
  1529.                     (isdigit (wParam) ? '0' : 'A' - 10)) ;
  1530.        else
  1531.          MessageBeep (0) ;
  1532.      }
  1533.      else                                    /* operation */
  1534.      {
  1535.        if (!bNewNumber)
  1536.          ShowNumber (hWnd, dwNumber =
  1537.                     CalcIt (dwFirstNum, nOperation, dwNumber)) ;
  1538.        bNewNumber = TRUE ;
  1539.        nOperation = wParam ;
  1540.      }
  1541.      break ;
  1542.  
  1543.  
  1544.     case WM_DESTROY:
  1545.       hWndCalc = NULL;
  1546.       break;
  1547.  
  1548.     default :
  1549.        return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  1550.   }
  1551.  
  1552.   return 0L ;
  1553. }
  1554.  
  1555.  
  1556. /*===========================================================================*/
  1557. /*                                                                           */
  1558. /*  POPTEST.C - demonstration of the usage of floating popups                */
  1559. /*                                                                           */
  1560. /*===========================================================================*/
  1561. VOID DoFloatingPopup(VOID)
  1562. {
  1563.   HMENU hMenu;
  1564.  
  1565.   if ((hMenu = LoadMenu(hAppInstance, (LPSTR) "FloatingPopup")) != NULL)
  1566.     TrackPopupMenu(GetSubMenu(hMenu,0), 0, MAKE_X(30),MAKE_Y(10), 0, hMain, 0);
  1567. }
  1568.  
  1569.  
  1570. /*===========================================================================*/
  1571. /*                                                                           */
  1572. /* File    : NOTEPAD.C                                                       */
  1573. /*                                                                           */
  1574. /* Purpose : Demo of a multi-line edit field plus a new kind of button       */
  1575. /*                                                                           */
  1576. /*===========================================================================*/
  1577. #define MAXBUFSIZE  0xF000
  1578.  
  1579. static HWND hWndNotepad, hEdit;
  1580. static LPSTR pszFileToOpen = NULL;
  1581.  
  1582. int NotePad(VOID)
  1583. {
  1584.   RECT r;
  1585.  
  1586.   if (hWndNotepad)
  1587.   {
  1588.     BringWindowToTop(hWndNotepad);
  1589.     SetFocus(hWndNotepad);
  1590.     return TRUE;
  1591.   }
  1592.  
  1593.   hWndNotepad = CreateWindow("NotePad",    /* class */
  1594.                              "NotePad",    /* title */
  1595.                              WS_BORDER | WS_CAPTION | WS_SYSMENU |
  1596.                              WS_THICKFRAME | WS_VISIBLE,
  1597.                              MAKE_X(5),  MAKE_Y(2),
  1598.                              MAKE_X(60), MAKE_Y(20),
  1599.                              hMain,          /* hParent */
  1600.                              NULL,           /* hMenu */
  1601.                              hAppInstance,   /* hInst */
  1602.                              0L);            /* lpParam */
  1603.  
  1604.   GetClientRect(hWndNotepad, (LPRECT) &r);
  1605.  
  1606.   hEdit = CreateWindow("Edit",         /* class */
  1607.                        NULL,           /* title */
  1608.                        ES_MULTILINE | WS_CHILD | WS_VISIBLE,  /* dwStyle */
  1609.                        0,0,
  1610.                        CX_RECT(r), CY_RECT(r),
  1611.                        hWndNotepad,    /* hParent */
  1612.                        0,              /* hMenu */
  1613.                        hAppInstance,   /* hInst */
  1614.                        0L);            /* lpParam */
  1615.  
  1616.   ShowWindow(hWndNotepad, SW_SHOW);
  1617.   UpdateWindow(hWndNotepad);
  1618.   BringWindowToTop(hWndNotepad);
  1619.   SetFocus(hEdit);
  1620.   return TRUE;
  1621. }
  1622.  
  1623.  
  1624. LONG FAR PASCAL NotepadWndProc(HWND hWnd, WORD message, WORD wParam, LONG lParam)
  1625. {
  1626.   char         buf[128];
  1627.   HANDLE       pHandle;
  1628.  
  1629.   switch (message)
  1630.   {
  1631.     case WM_COMMAND    :
  1632.       switch (wParam)
  1633.       {
  1634.         case ID_EXIT:
  1635.           hWndNotepad = NULL;
  1636.           DestroyWindow(hWnd);
  1637.           break;
  1638.  
  1639.         case ID_CUT :
  1640.           SendMessage(hEdit, WM_CUT, 0, 0L);
  1641.           break;
  1642.         case ID_CLEAR:
  1643.           SendMessage(hEdit, WM_CLEAR, 0, 0L);
  1644.           break;
  1645.         case ID_COPY:
  1646.           SendMessage(hEdit, WM_COPY, 0, 0L);
  1647.           break;
  1648.         case ID_PASTE:
  1649.           SendMessage(hEdit, WM_PASTE, 0, 0L);
  1650.           break;
  1651.  
  1652.         case ID_GETLEN :
  1653.           wsprintf(buf, "There are %d characters", 
  1654.                   (int) SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0L));
  1655.           MessageBox(hMain, buf, "Text Length", MB_OK);
  1656.           break;
  1657.         case ID_GETLINES:
  1658.           wsprintf(buf, "There are %d lines", 
  1659.                   (int) SendMessage(hEdit, EM_GETLINECOUNT, 0, 0L));
  1660.           MessageBox(hMain, buf, "# of Lines", MB_OK);
  1661.           break;
  1662.         case ID_GETHANDLE:
  1663.           pHandle = (HANDLE) SendMessage(hEdit, EM_GETHANDLE, 0, 0L);
  1664.           wsprintf(buf, "The edit handle is [%x]", pHandle);
  1665.           MessageBox(hMain, buf, "Get Handle", MB_OK);
  1666.           break;
  1667.       }
  1668.       break;
  1669.  
  1670.     case WM_SIZE  :
  1671.       MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  1672.       break;
  1673.  
  1674.     case WM_CLOSE :
  1675.       hWndNotepad = NULL;
  1676.       DestroyWindow(hWnd);
  1677.       return TRUE;
  1678.  
  1679.     default :
  1680.       /* Call the default window procedure for the main window */
  1681.       return DefWindowProc(hWnd, message, wParam, lParam);
  1682.   }
  1683.   return TRUE;
  1684. }
  1685.  
  1686.  
  1687. /*==========================================================================*/
  1688. /*                                                                          */
  1689. /* Function : ComboDemo()                                                   */
  1690. /*                                                                          */
  1691. /* Purpose  : Shows all three styles of combo boxes                         */
  1692. /*                                                                          */
  1693. /* Returns  :                                                               */
  1694. /*                                                                          */
  1695. /*==========================================================================*/
  1696. #define ID_CBFUTURES                      1002
  1697. #define ID_CBEQUITIES                     1003
  1698. #define ID_CBTICK                         1004
  1699.  
  1700. VOID ComboDemo(void)
  1701. {
  1702.   FARPROC lpfn;
  1703.  
  1704.   MessageBox(hMain,
  1705.             "To pulldown the hidden listbox portion of a combobox,\
  1706. click on the combobox arrow icon or press F4 or ALT-GREY-DOWN.",
  1707.                      "Combo box demo", MB_OK);
  1708.  
  1709.   lpfn = MakeProcInstance((FARPROC) ComboDialogProc, hAppInstance);
  1710.   DialogBox(hAppInstance, "UpdateSettings", hMain, lpfn);
  1711.   FreeProcInstance(lpfn);
  1712. }
  1713.  
  1714.  
  1715. int FAR PASCAL ComboDialogProc(HWND hDlg, WORD msg, WORD wParam, DWORD lParam)
  1716. {
  1717.   int i;
  1718.  
  1719.   static char *szColors[] =
  1720.   {
  1721.     "Black", "Blue",    "Green",  "Cyan", 
  1722.     "Red",   "Magenta", "Yellow", "White",
  1723.     NULL
  1724.   };
  1725.  
  1726.  
  1727.   (void) lParam;
  1728.  
  1729.   switch (msg)
  1730.   {
  1731.     case WM_INITDIALOG :
  1732.       SendDlgItemMessage(hDlg, ID_CBTICK, WM_SETREDRAW, FALSE, 0L);
  1733.       for (i = 0;  szColors[i];  i++)
  1734.         SendDlgItemMessage(hDlg, ID_CBTICK, CB_ADDSTRING, 0, (DWORD) (LPSTR) szColors[i]);
  1735.       SendDlgItemMessage(hDlg, ID_CBTICK, CB_SETCURSEL, 2, 0L);
  1736.       i = (int) SendDlgItemMessage(hDlg, ID_CBTICK, CB_GETCURSEL, 0, 0L);
  1737.       SendDlgItemMessage(hDlg, ID_CBTICK, WM_SETREDRAW, TRUE, 0L);
  1738.  
  1739.       SendDlgItemMessage(hDlg, ID_CBFUTURES, WM_SETREDRAW, FALSE, 0L);
  1740.       for (i = 0;  szColors[i];  i++)
  1741.         SendDlgItemMessage(hDlg, ID_CBFUTURES, CB_ADDSTRING, 0, (DWORD) (LPSTR) szColors[i]);
  1742.       SendDlgItemMessage(hDlg, ID_CBFUTURES, CB_SETCURSEL, 6, 0L);
  1743.       i = (int) SendDlgItemMessage(hDlg, ID_CBFUTURES, CB_GETCURSEL, 0, 0L);
  1744.       SendDlgItemMessage(hDlg, ID_CBFUTURES, WM_SETREDRAW, TRUE, 0L);
  1745.  
  1746.       SendDlgItemMessage(hDlg, ID_CBEQUITIES, WM_SETREDRAW, FALSE, 0L);
  1747.       for (i = 0;  szColors[i];  i++)
  1748.         SendDlgItemMessage(hDlg, ID_CBEQUITIES, CB_ADDSTRING, 0, (DWORD) (LPSTR) szColors[i]);
  1749.       SendDlgItemMessage(hDlg, ID_CBEQUITIES, CB_SETCURSEL, 2, 0L);
  1750.       i = (int) SendDlgItemMessage(hDlg, ID_CBEQUITIES, CB_GETCURSEL, 0, 0L);
  1751.       SendDlgItemMessage(hDlg, ID_CBEQUITIES, WM_SETREDRAW, TRUE, 0L);
  1752.       return TRUE;
  1753.  
  1754.     case WM_COMMAND :
  1755.       switch (wParam)
  1756.       {
  1757.         case IDOK :
  1758.         case IDCANCEL :
  1759.           EndDialog(hDlg, TRUE);
  1760.           break;
  1761.       }
  1762.       return TRUE;
  1763.   }
  1764.   return FALSE;
  1765. }
  1766.  
  1767.